package com.softstao.guoyu.ui.activity.me;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.alertview.AlertView;
import com.softstao.guoyu.R;
import com.softstao.guoyu.base.BaseActivity;
import com.softstao.guoyu.global.SharePreferenceManager;
import com.softstao.guoyu.global.UserManager;
import com.softstao.guoyu.model.me.PersonalInfo;
import com.softstao.guoyu.model.me.User;
import com.softstao.guoyu.model.me.UserCondition;
import com.softstao.guoyu.mvp.interactor.me.UserInfoInteractor;
import com.softstao.guoyu.mvp.presenter.me.UserInfoPresenter;
import com.softstao.guoyu.mvp.viewer.me.UpdateInfoViewer;
import com.softstao.guoyu.ui.activity.MainActivity;
import com.softstao.softstaolibrary.library.utils.LZUtils;
import com.softstao.softstaolibrary.library.widget.LZToast;
import com.wangjie.androidinject.annotation.annotations.mvp.AIPresenter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by vaikus on 2017/6/7.
 */

public class UserInfoEditActivity extends BaseActivity implements UpdateInfoViewer {

    @AIPresenter(presenter = UserInfoPresenter.class, interactor = UserInfoInteractor.class)
    UserInfoPresenter presenter;

    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.idCardNo)
    EditText idCardNo;
    @BindView(R.id.sex)
    TextView sex;
    @BindView(R.id.wechat)
    EditText wechat;
    @BindView(R.id.qq)
    EditText qq;
    @BindView(R.id.payee)
    EditText payee;
    @BindView(R.id.accountPayee)
    EditText accountPayee;
    @BindView(R.id.openingBank)
    EditText openingBank;
    @BindView(R.id.loading)
    RelativeLayout loading;
    private AlertView alertView;
    private User user;
    private UserCondition condition = new UserCondition();
    private int sex_num;

    @Override
    public int _ContentView() {
        return R.layout.activity_user_info_edit;
    }

    @Override
    public void initView() {
        initTitle("完善资料");
        user = UserManager.getInstance().getUser();
        initData();
    }

    private void initData() {
        name.setText(user.getName());
        idCardNo.setText(user.getIdCardNo());
        switch (user.getSex()) {
            case 1:
                sex.setText("男");
                break;
            case 2:
                sex.setText("女");
                break;
        }
        wechat.setText(user.getWechat());
        qq.setText(user.getQq());
        payee.setText(user.getPayee());
        accountPayee.setText(user.getAccountPayee());
        openingBank.setText(user.getOpeningBank());
        if (user.getIsAlreadyUpinfo()==1){
            name.setEnabled(false);
            idCardNo.setEnabled(false);
            wechat.setEnabled(false);
            qq.setEnabled(false);
        }else {
            name.setEnabled(true);
            idCardNo.setEnabled(true);
            wechat.setEnabled(true);
            qq.setEnabled(true);
        }
    }

    @OnClick({R.id.submit_btn, R.id.sex})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.submit_btn:
                condition.setName(name.getText().toString());
                condition.setIdCardNo(idCardNo.getText().toString());
                condition.setWechat(wechat.getText().toString());
                condition.setQq(qq.getText().toString());
                condition.setPayee(payee.getText().toString());
                condition.setAccountPayee(accountPayee.getText().toString());
                condition.setOpeningBank(openingBank.getText().toString());
                if (!check()){
                    return;
                }else {
                    if (UserManager.getInstance().getUser().getIsAlreadyUpinfo()==0){
                        notice();
                    }else {
                        updateInfo();
                    }
                }
                break;
            case R.id.sex:
                if (user.getIsAlreadyUpinfo()==0) {
                    View v = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.sex_edit_layout, null, false);
                    alertView = new AlertView(null, null, null, null, null, this, AlertView.Style.Alert, null).addExtView(v).setCancelable(true);
                    RadioButton male = (RadioButton) v.findViewById(R.id.male);
                    RadioButton female = (RadioButton) v.findViewById(R.id.female);
                    if (user.getSex() != 0) {
                        if (user.getSex() == 1) {
                            male.setChecked(true);
                            female.setChecked(false);
                        } else {
                            male.setChecked(false);
                            female.setChecked(true);
                        }
                    } else {
                        male.setChecked(false);
                        female.setChecked(false);
                    }
                    ((RadioGroup) v.findViewById(R.id.sex_edit)).setOnCheckedChangeListener((group, checkedId) -> {
                        switch (checkedId) {
                            case R.id.male:
                                sex.setText(((RadioButton) group.findViewById(R.id.male)).getText().toString());
                                sex_num = 1;
                                break;
                            case R.id.female:
                                sex.setText(((RadioButton) group.findViewById(R.id.female)).getText().toString());
                                sex_num = 2;
                                break;
                        }
                        user.setSex(sex_num);
                        alertView.dismiss();
                        condition.setSex(sex_num);
                    });
                    alertView.show();
                }
                break;
        }
    }

    private boolean check(){
        if (TextUtils.isEmpty(name.getText())){
            LZToast.getInstance(context).showToast("请输入姓名");
            return false;
        }if (TextUtils.isEmpty(idCardNo.getText())){
            LZToast.getInstance(context).showToast("请输入身份证号");
            return false;
        } if (idCardNo.length() != 15 && idCardNo.length() != 18) {
            LZToast.getInstance(context).showToast("身份证号长度应该为15位或18位");
            return false;
        }if (TextUtils.isEmpty(sex.getText())){
            LZToast.getInstance(context).showToast("请选择性别");
            return false;
        }if (TextUtils.isEmpty(wechat.getText())){
            LZToast.getInstance(context).showToast("请输入微信号");
            return false;
        }if (TextUtils.isEmpty(qq.getText())){
            LZToast.getInstance(context).showToast("请输入QQ号");
            return false;
        }
        return true;
    }

    private void notice(){
        Dialog dialog = new Dialog(context,R.style.MyDialog);
        dialog.setContentView(R.layout.layout_info_notice);
        dialog.getWindow().setLayout(getScreenWidth() - LZUtils.dipToPix(context, 30) * 2, ViewGroup.LayoutParams.WRAP_CONTENT);
        ImageView cancelIc = (ImageView) dialog.findViewById(R.id.cancel_ic);
        TextView cancel = (TextView) dialog.findViewById(R.id.cancel_btn);
        TextView confirm = (TextView) dialog.findViewById(R.id.confirm_btn);
        cancelIc.setOnClickListener(v -> dialog.dismiss());
        cancel.setOnClickListener(v -> dialog.dismiss());
        confirm.setOnClickListener(v -> updateInfo());
        dialog.show();
    }

    @Override
    public void updateInfo() {
        loading.setVisibility(View.VISIBLE);
        condition.setAgentId(SharePreferenceManager.getInstance().getAgentId());
        presenter.updateInfo(condition);
    }

    @Override
    public void updateResult(PersonalInfo personalInfo) {
        loading.setVisibility(View.GONE);
        UserManager.getInstance().setUser(personalInfo.getPersonalInfo());
        startActivity(new Intent(context, MainActivity.class).putExtra("index","4"));
        finish();
    }

    @Override
    protected void error(String msg) {
        super.error(msg);
        loading.setVisibility(View.GONE);
    }
}
